using System.Collections.Generic;
using System.Text;

namespace Musi.Core.CSV
{
    public class CSVWriter
    {
        private List<List<string>> datas;

        public CSVWriter()
        {
            datas = new List<List<string>>();
        }

        /// <summary>
        /// This line start by number 1, and do so for column.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        public void AppendElement(ICSVSerializable data, int row)
        {
            if (datas.Count < row)
            {
                int offsetRow = row - datas.Count;
                for (int addCount = 1; addCount <= offsetRow; addCount++)
                {
                    datas.Add(null);
                }
            }

            if (datas[row - 1] == null)
            {
                datas[row - 1] = new List<string>();
            }

            datas[row - 1].Add(data.CSVSerialize());
        }

        public void InsertElement(ICSVSerializable data, int row, int column)
        {
            if (datas.Count < row)
            {
                int offsetRow = row - datas.Count;
                for (int addCount = 1; addCount <= offsetRow; addCount++)
                {
                    datas.Add(null);
                }
            }

            if (datas[row - 1] == null)
            {
                datas[row - 1] = new List<string>();
            }

            datas[row - 1].Insert(column - 1, data.CSVSerialize());
        }
    }
}